package com.example.moretherz.demoa3_ds0926;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDelListener,IOnEdtListener {
    private long lastPressTime = 0 ;
    private final String TAG = "MainActivity";
    private ListView stuListView;
    /*private String nameArr[] = {"渣渣辉","乌蝇哥","托尼哥"};*/
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchResult = new ArrayList<>();
    private Myadpter adapter;
    private Button btnAddStu;
    private EditText edtKeyWords;
    private Button btnSearch;
    Boolean isResultList =false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initSearch();

        //初始化学生列表
        initStuList();
        stuListView = findViewById(R.id.lv_stu);

        adapter = new Myadpter(MainActivity.this,studentArrayList,MainActivity.this,MainActivity.this);
/*
        adapter = new ArrayAdapter<String>(
                MainActivity.this,
                R.layout.my_list_item,
                nameArr
        );
*/
        stuListView.setAdapter(adapter);

        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this,
                        "姓名"+studentArrayList.get(i).getStudentName() + " 年龄"
                                +  studentArrayList.get(i).getAge(),
                        Toast.LENGTH_SHORT)
                        .show();
            }
        });

        btnAddStu = findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,
                        AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });


    }

    private void initSearch() {
        edtKeyWords = findViewById(R.id.keywords);
        btnSearch = findViewById(R.id.search_button);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword = edtKeyWords.getText().toString().trim();
                search(studentArrayList,keyword);
                adapter.changeData(searchResult);
                isResultList = true;
            }
        });
        edtKeyWords.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = edtKeyWords.getText().toString().trim();
                search(studentArrayList,keyword);
                adapter.changeData(searchResult);
            }
        });
    }

    private void search(ArrayList studentArrayList,String keyword) {
        searchResult.clear();
        for(int i =0;i<studentArrayList.size();i++){
         Student s = (Student) studentArrayList.get(i);
         if(s.getStudentName().contains(keyword)){
             searchResult.add(s);
         }


        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("MainActivity",
                "requestCode = " + requestCode + " resultCode = " + resultCode);

        if(data == null){
            return;
        }
        if(requestCode==2001) {
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.dog1);

            Student s = new Student(imgId, name, age);
            studentArrayList.add(s);
            adapter.notifyDataSetChanged();
        }
        else if(requestCode==3001){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.dog1);
            int index = data.getIntExtra("INDEX", 0);

            Student s = studentArrayList.get(index);
            s.setStudentName(name);
            s.setAge(age);
            s.setImgID(imgId);

            adapter.notifyDataSetChanged();
        }
    }

    private void initStuList() {
        Student s1 = new Student(R.drawable.dog1,"渣渣辉",18);
        Student s2 = new Student(R.drawable.dog2,"乌蝇哥",19);
        Student s3 = new Student(R.drawable.dog3,"托尼哥",20);

        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
    }

    @Override
    public void onBackPressed() {
        long currentPressTime = System.currentTimeMillis();
        if(currentPressTime-lastPressTime>2000){
            lastPressTime=currentPressTime;
            Toast.makeText(MainActivity.this,"再按一次退出",Toast.LENGTH_SHORT).show();
        }
        else{
            super.onBackPressed();
        }

}

    @Override
    public void del(int i) {
        Log.e("MainActivity","del"+i);
        studentArrayList.remove(i);
        adapter.notifyDataSetInvalidated();
    }

    @Override
    public void edit(int i) {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,EditActivity.class);
        intent.putExtra("NAME",studentArrayList.get(i).getStudentName());
        intent.putExtra("AGE",studentArrayList.get(i).getAge());
        intent.putExtra("ImgID",studentArrayList.get(i).getImgID());
        intent.putExtra("INDEX", i);
        startActivityForResult(intent, 2002);
    }
}
